/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Ngoc Vien
 */
public class Controller extends HttpServlet {
    private boolean found = false;
    final String khonghople = "khonghople.jsp";
    final String trangchu = "trangchu.jsp";
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("btAction");
            if (action.equals("dangnhap")) {
                System.out.println("Controller: dangnhap");
                String username = request.getParameter("txtUsername");
                String password = request.getParameter("txtPassword");
                System.out.println(username);
                System.out.println(password);                                
                
                try {
                    SAXParserFactory spf = SAXParserFactory.newInstance();
                    SAXParser saxparser = spf.newSAXParser();
                    MySaxParser sacobj = new MySaxParser(username,password);
                    String path = getServletContext().getRealPath("/");
                    saxparser.parse(new File(path+ "WEB-INF/hoso01.xml"), sacobj);
                    System.out.println("Parsed!");
                    if (sacobj.isFound()) {
                        HttpSession session = request.getSession();
                        session.setAttribute("USERNAME", username);                                                
                        RequestDispatcher rd = request.getRequestDispatcher(trangchu);
                        rd.forward(request, response);
                    } else {                                                
                        RequestDispatcher rd = request.getRequestDispatcher(khonghople);
                        rd.forward(request, response);
                    }
                } catch (ParserConfigurationException ex) {
                    System.out.println(ex.getCause());
                } catch (SAXException ex) {
                    System.out.println(ex.getCause());
                } catch (IOException ex) {
                    System.out.println(ex.getCause());
                }
                                
                

            }//end if Login
//            else if(action.equals("Search"))
//            {
//                String name =request.getParameter("txtName");
//                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//                DocumentBuilder db = dbf.newDocumentBuilder();
//                String path = getServletContext().getRealPath("/");
//                Document doc = db.parse(new File(path+"WEB-INF/account.xml"));
//                XPathFactory xpf = XPathFactory.newInstance();
//                javax.xml.xpath.XPath xpath = xpf.newXPath();
//                String sql = "//student[contains(username,'"+name+"')]/*";
//                NodeList list = (NodeList) xpath.evaluate(sql, doc, XPathConstants.NODESET);
//                List result =  new ArrayList();
//                String username = "";
//                for(int i=0; i <list.getLength();i++ )
//                {
//                    Node tmp =list.item(i);
//                    if(tmp.getNodeName().equals("username"))
//                    {
//                        username = tmp.getTextContent().trim();
//                    }
//                    if(tmp.getNodeName().equals("password"))
//                    {
//                        AccountDTO acc = new AccountDTO(username,tmp.getTextContent().trim());
//                        result.add(acc);
//                    }
//                }
//                request.setAttribute("INFO", result);
//                RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
//                rd.forward(request, response);
//
//            }
//        } catch (XPathExpressionException ex) {
//            ex.printStackTrace();
        }catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }



//        catch (SAXException ex) {
//            ex.printStackTrace();
//        } catch (ParserConfigurationException ex) {
//            ex.printStackTrace();
//        } finally {
//            out.close();
//        }
    }

    private void dangnhap()
    {
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
